/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PanInputModelExpression.java
 *
 * Created on 31-jul-2011, 9:50:46
 */

package br.usp.ime.faguilar.guis.capturers;

import DatabaseMathExpressions.Category;
import DatabaseMathExpressions.DBFuntions;
import DatabaseMathExpressions.ModelExpression;
import DatabaseMathExpressions.ModelExpressionGroundTruth;
import br.usp.ime.faguilar.guis.MathExpressionDrawing.Panels.PanWriting;
import br.usp.ime.faguilar.guis.PanLabeling;
import br.usp.ime.faguilar.guis.PanModelExpressions;
import MathExpression.Data.DMathExpression;
import MathExpression.Graphics.GMathExpression;
import java.awt.Component;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JDialog;
import javax.swing.JList;

/**
 *
 * @author frank
 */
public class PanInputModelExpression extends javax.swing.JPanel {

    private PanLabeling panFather;

    private final static int MAX_HEIGHT_OF_IMAGES=50;

    private DBFuntions dbFunctions;
    
    private ModelExpressionGroundTruth textualRepresentations;

    private ArrayList<Category> categories;

    /** Creates new form PanInputModelExpression */
    public PanInputModelExpression() {
        initComponents();
        textualRepresentations=new ModelExpressionGroundTruth();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        panControlInputMathExpressions1 = new br.usp.ime.faguilar.guis.capturers.PanControlInputMathExpressions();
        jPanel1 = new javax.swing.JPanel();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        newCategory = new javax.swing.JButton();
        expressionLevelGroundTruth = new br.usp.ime.faguilar.guis.ModelExpressionGroundTruthEditor();
        jPanel2 = new javax.swing.JPanel();
        butSaveModel = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        AllModelsButton = new javax.swing.JButton();

        setLayout(new java.awt.BorderLayout());
        add(panControlInputMathExpressions1, java.awt.BorderLayout.CENTER);

        jComboBox1.setRenderer(new DefaultListCellRenderer() {
            @Override
            public Component getListCellRendererComponent(
                JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                if (value instanceof Category) {
                    Category category = (Category)value;
                    setText(category.getName());
                }
                return this;
            }
        });

        jLabel1.setText("Category:");

        newCategory.setText("+");
        newCategory.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                newCategoryActionPerformed(evt);
            }
        });

        expressionLevelGroundTruth.setBorder(javax.swing.BorderFactory.createTitledBorder("Textual Representation"));

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(29, 29, 29)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel1)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 234, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(newCategory, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 44, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(expressionLevelGroundTruth, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 747, Short.MAX_VALUE))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(expressionLevelGroundTruth, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 187, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jComboBox1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1)
                    .add(newCategory))
                .add(10, 10, 10))
        );

        add(jPanel1, java.awt.BorderLayout.PAGE_START);

        butSaveModel.setText("Save");
        butSaveModel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                butSaveModelMouseClicked(evt);
            }
        });

        jSeparator1.setForeground(new java.awt.Color(102, 102, 102));

        AllModelsButton.setText("All models");
        AllModelsButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                AllModelsButtonMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 793, Short.MAX_VALUE)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel2Layout.createSequentialGroup()
                .add(20, 20, 20)
                .add(AllModelsButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 125, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 556, Short.MAX_VALUE)
                .add(butSaveModel)
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(butSaveModel)
                    .add(AllModelsButton))
                .addContainerGap())
        );

        add(jPanel2, java.awt.BorderLayout.PAGE_END);
    }// </editor-fold>//GEN-END:initComponents

    private void butSaveModelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_butSaveModelMouseClicked
        // TODO add your handling code here:
//        if(!this.txtAreaTextualRepresentation.getText().isEmpty())
//            addText();
        ModelExpression me=new ModelExpression(0);
        me.setTextualRepresentation(expressionLevelGroundTruth.getGroundTruth());
        me.setdMathExpression((DMathExpression) this.panControlInputMathExpressions1.getGMathExpression().clone());
        String category=categories.get(jComboBox1.getSelectedIndex()).getName();
        me.setCategoryName(category);
        Point2D pLocation=panControlInputMathExpressions1.getPanWriting().getLocationOnScreen();
        GMathExpression gme=panControlInputMathExpressions1.getGMathExpression();
        pLocation.setLocation(pLocation.getX()+gme.getLtPoint().getX()-2,
                    pLocation.getY()+gme.getLtPoint().getY()-2);
        dbFunctions.addModelExpressionAndGetGeneratedKey(me);
        panFather.chargeModelsJList();
        this.panControlInputMathExpressions1.clearPanel();
        expressionLevelGroundTruth.initializeCustomFields();
    }//GEN-LAST:event_butSaveModelMouseClicked

    private void AllModelsButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_AllModelsButtonMouseClicked
        // TODO add your handling code here:
        PanModelExpressions panModelExpressions=new PanModelExpressions();
        panModelExpressions.setDatabaseFunctions(dbFunctions);
        panModelExpressions.chargeModelExpressions();
        
        JDialog dialog=new JDialog();
        dialog.setContentPane(panModelExpressions);
        dialog.pack();
        dialog.setModal(true);
        dialog.setVisible(true);
    }//GEN-LAST:event_AllModelsButtonMouseClicked


    private void newCategoryActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newCategoryActionPerformed
        // TODO add your handling code here:
        JDialog newCategoryDialog=new JDialog();
        PanNewCategory panNewCategory=new PanNewCategory();
        panNewCategory.setInpuModel(this);
        panNewCategory.setDatabaseFunctions(dbFunctions);
        newCategoryDialog.setContentPane(panNewCategory);
        newCategoryDialog.pack();
        newCategoryDialog.setModal(true);
        newCategoryDialog.setResizable(false);
        newCategoryDialog.setVisible(true);
        
    }//GEN-LAST:event_newCategoryActionPerformed

    public void chargeCategories(){
        categories=dbFunctions.getCategories();
        jComboBox1.removeAllItems();
        for (Category category : categories) {
            jComboBox1.addItem(category.getName());
        }
        this.jComboBox1.repaint();
    }

    public PanWriting getwritingPanel(){
        return this.panControlInputMathExpressions1.getPanWriting();
    }
//    public void addText(){
//        if(txtAreaTextualRepresentation.getText().isEmpty()){
//            //mostrar dialogo de panel vacio
//            JOptionPane.showMessageDialog(this,"Please write a textual representation","No textual representation",
//                                JOptionPane.ERROR_MESSAGE);
//        }else{
//            String newTextualRepresentation=txtAreaTextualRepresentation.getText();
//            textualRepresentations.add(newTextualRepresentation);
//            txtAreaTextualRepresentation.setText("");
//        }
//    }

    public PanControlInputMathExpressions getPanControlInputMathExpressions1() {
        return panControlInputMathExpressions1;
    }

    public void setPanControlInputMathExpressions1(PanControlInputMathExpressions panControlInputMathExpressions1) {
        this.panControlInputMathExpressions1 = panControlInputMathExpressions1;
    }


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton AllModelsButton;
    private javax.swing.JButton butSaveModel;
    private br.usp.ime.faguilar.guis.ModelExpressionGroundTruthEditor expressionLevelGroundTruth;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton newCategory;
    private br.usp.ime.faguilar.guis.capturers.PanControlInputMathExpressions panControlInputMathExpressions1;
    // End of variables declaration//GEN-END:variables

    public PanLabeling getPanFather() {
        return panFather;
    }

    public void setPanFather(PanLabeling panFather) {
        this.panFather = panFather;
    }

    public DBFuntions getDbFunctions() {
        return dbFunctions;
    }

    public void setDbFunctions(DBFuntions dbFunctions) {
        this.dbFunctions = dbFunctions;
    }


}
